﻿using System.Diagnostics.CodeAnalysis;

namespace c4
{
    internal class Program
    {
        //结点定义
        public class Node<T>
        {
            public Node<T> Next { get; set; }
            public T Data { get; set; }

            public Node(T Data)
            {
                this.Data = Data;
                Next = null;
            }
        }

        //泛式链表类
        public class List<T>
        {
            private Node<T> head;
            private Node<T> tail;

            public List()
            {
                head = tail = null;
            }

            public void Add(T t)
            {
                Node<T> n = new Node<T>(t);
                if(tail == null)
                { 
                    head = tail = n;
                }
                else
                {
                    tail.Next = n;
                    tail = n;
                }
            }

            public void ForEach(Action<T> action)
            {
                //?表示可以是null，防止报错
                Node<T>? node = head;
                while(node != null)
                {
                    action(node.Data);
                    node = node.Next;
                }
            }
        }
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(4);
            list.Add(5);
            list.Add(2);
            list.Add(10);
            list.Add(3);


            //求最大值
            int max = int.MinValue;
            list.ForEach(item => { if (item > max) max = item; });
            Console.WriteLine("最大值"+max);

            int min = int.MaxValue;
            list.ForEach(item => { if (item < min) min = item; });
            Console.WriteLine("最小值" + min);

            int sum = 0;
            list.ForEach(item => { sum += item; });
            Console.WriteLine("和为" + sum);
        }
    }
}
